package cn.edu.zxj.rabbitmqmyself.controller;

import cn.edu.zxj.rabbitmqmyself.model.compile.and.run.Response;
import cn.edu.zxj.rabbitmqmyself.model.info.ProblemInfo;
import cn.edu.zxj.rabbitmqmyself.service.ProblemService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description：
 *
 * @author: zxj
 * @date: 2024-10-02
 * @time: 01:59:37
 */
@Slf4j
@RestController
public class ProblemController {
    @Autowired
    private ProblemService problemService;

    @RequestMapping("/problem/selectAllProblems")
    public List<ProblemInfo> selectAllProblems() {
        log.info("接收到 selectAllProblems 请求");
        return problemService.selectAllProblems();
    }

    @RequestMapping("/problem/selectProblemById")
    public ProblemInfo selectProblemById(Integer problemId) {
        log.info("接收到 selectProblemById 请求, problemId={}", problemId);
        return problemService.selectProblemById(problemId);
    }

    /**
     * 用来提供编译和运行的功能
     *
     * @param problemId 问题的 id, 用来查询问题的信息
     * @param code      用户提交的代码
     * @return
     */
    @RequestMapping("compileAndRun")
    public Response compileAndRun(Integer problemId, String code) {
        log.info("接收到 compileAndRun 请求,problemId={}, code={}",problemId, code);

        // 提高容错, 参数的合法性校验
        if (problemId == null || code == null || code.trim().isEmpty()) {
            return Response.error("参数不合法");
        }

        Response response = problemService.compileAndRun(problemId, code);

        if (response == null) {
            return Response.error("系统内部错误");
        }


        return response;
    }

}
